package com.graduation.rescuematerial.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/14 16:52
 * @Version:
 * @Description:拦截器
 */
//@Component
public class SampleInterceptor implements HandlerInterceptor {


//    @Autowired
//    private UserService userService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("已进入拦截器");

        String requestURL=request.getRequestURI();
        System.out.println("拦截路径："+requestURL);
        Cookie[] cookies=request.getCookies();


        if(cookies!=null && cookies.length!=0){
            for(Cookie cookie:cookies){
//                System.out.println("cookies="+cookie.getName());
                if(cookie.getName().equals("userTicket")){
                    return true;
                }
            }
        }
//        String ticket= CookieUtil.getCookieValue(request,"userTicket");
//        //没有cookie，非法登录
//        if(StringUtils.isEmpty(ticket)){
////            throw new GlobalException(RespBeanEnum.ILLEGAL_OPERATION);
//            return false;
//        }


        request.setAttribute("msg","请先登录");
//        request.getRequestDispatcher("/").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("控制器执行完毕");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("得到返回结果："+response);
        System.out.println("请求完毕");
    }


}
